package itemCF.step2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.net.URI;

/**
 * @author legolas
 * @date 2020/3/20 上午12:20
 * 利用评分矩阵，构建物品与物品的相似度矩阵
 * 输入：步骤1的输出
 * 缓存：步骤1的输出（与输入是同一个文件）
 * 输出：用户ID(行)    物品ID(列)_相似度
 */
public class Step2Job {
    private static String inPath = "/itemCF/step1_output/";
    private static String outPath = "/itemCF/step2_output/";
    private static String cache = "/itemCF/step1_output/***";
    private static String hdfs = "hdfs://hadoopmaster:9000";

    public static void main(String[] args) {
        try {
            //创建Job需要的配置参数
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", hdfs);
            //创建一个job
            Job job = Job.getInstance(conf);

            //设置缓存
            job.addCacheFile(new URI(cache + "#itemUserScore2"));

            job.setJarByClass(Step2Job.class);
            //指定输入路径
            FileInputFormat.setInputPaths(job, new Path(inPath));
            FileOutputFormat.setOutputPath(job, new Path(outPath));
            //指定map相关代码
            job.setMapperClass(Mapper2.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);

            //指定reduce相关代码
            job.setReducerClass(Reducer2.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);

            //提交job
            job.waitForCompletion(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
